package principale.presentation.gui.interfacciaGraficaPura;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.LayoutManager;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import principale.main.Main;
import principale.presentation.gui.modelli.ModelloElencoUtenti;
import principale.presentation.gui.operazioniLogiche.AscoltatoreButtonGestione;

public class GestioneUtentiGUI implements ListSelectionListener {
	
	private static ModelloElencoUtenti modelloUtenti;
	private static JList elenco;
	private static JFrame frame;
	private JPanel p;
	
	public GestioneUtentiGUI() {
		if (frame!=null) {
			frame.dispose();
		}
		frame=new JFrame("Gestione utenti");
		creaElenco();
		creaPannelloButton();
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	protected void creaElenco() {
		modelloUtenti=new ModelloElencoUtenti();
		elenco = new JList(modelloUtenti);
		elenco.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		elenco.setLayoutOrientation(JList.HORIZONTAL_WRAP);
		//list.setVisibleRowCount(-1);
		JScrollPane listScroller = new JScrollPane(elenco);
		listScroller.setPreferredSize(new Dimension(250, 80));
		frame.add(listScroller, BorderLayout.PAGE_START);
	}
	protected void creaPannelloButton() {
		p=new JPanel();
		p.setLayout((LayoutManager)new BoxLayout(p,BoxLayout.LINE_AXIS));
		JButton button = new JButton("Aggiungi utente");
		button.addActionListener(new AscoltatoreButtonGestione(0));
		p.add(button);
		button = new JButton("Modifica utente");
		button.addActionListener(new AscoltatoreButtonGestione(1));
		p.add(button);
		button = new JButton("Elimina utente");
		button.addActionListener(new AscoltatoreButtonGestione(2));
		p.add(button);
		frame.add(p,BorderLayout.PAGE_END);
	}
	
	public void mostraGUI() {
		frame.pack();
		Main.centralizzaFinestra(frame);
		frame.setVisible(true);
	}
	
	public static ModelloElencoUtenti getModelloElenco() {
		return modelloUtenti;
	}
	
	public static JList getElenco() {
		return elenco;
	}
	
	public static JFrame getFrame() {
		return frame;
	}

	@Override
	public void valueChanged(ListSelectionEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}
